package com.pearadmin.pro.modules.ser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.base.page.Pageable;
import com.pearadmin.pro.modules.ser.domain.ServerDept;
import com.pearadmin.pro.modules.ser.domain.ServerInfo;
import com.pearadmin.pro.modules.ser.domain.dto.ServerTreeDto;
import com.pearadmin.pro.modules.ser.param.ServerInfoRequest;
import com.pearadmin.pro.modules.ser.pojo.WebSSHData;
import com.pearadmin.pro.modules.ser.repository.ServerInfoRepository;
import com.pearadmin.pro.modules.ser.service.ServerDeptService;
import com.pearadmin.pro.modules.ser.service.ServerInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: didiplus
 * Email: 972479352@qq.com
 * CreateTime: 2022/3/5
 * Desc:
 */

@Service
public class ServerInfoServiceImpl extends ServiceImpl<ServerInfoRepository, ServerInfo> implements ServerInfoService {

    @Resource
    ServerInfoRepository serverInfoRepository;

    @Resource
    ServerDeptService serverDeptService;

    @Override
    public List<ServerInfo> list(ServerInfoRequest request) {
        return serverInfoRepository.selectServerInfo(request);
    }

    @Override
    public PageResponse<ServerInfo> page(ServerInfoRequest request) {
        return Pageable.of(request,(()-> serverInfoRepository.selectServerInfo(request)));
    }


    /**
     * 根据部门ID获取所属部门的所有服务器
     * @param deptId
     */
    @Override
    public ServerTreeDto getServerTree(String deptId) {
        List<ServerInfo> serverInfos = serverInfoRepository.selectServerByDeptId(deptId);
        List  serverList = new ArrayList<>();
        serverInfos.forEach((item)->{
            Map<String, String> map = new HashMap<>();
            map.put("title",item.getIp());
            map.put("key",item.getId());
            serverList.add(map);
        });
        ServerTreeDto serverTreeDto = new ServerTreeDto();
        serverTreeDto.setTitle("顶级目录");
        serverTreeDto.setKey(deptId);
        serverTreeDto.setChildren(serverList);
        return serverTreeDto;
    }

    @Override
    public WebSSHData selectserverById(String hostId) {
        return serverInfoRepository.selectServerById(hostId);
    }

}
